package com.lvyq.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.lvyq.cache.CacheManager;
import com.lvyq.common.MsgTypeConstant;
import com.lvyq.common.OrderTypeConstant;
import com.lvyq.model.order.Order;
import com.lvyq.model.receive.ReceiveMsg;
import com.lvyq.model.reply.ReplyMsg;
import com.lvyq.repository.OrderRepository;
import com.lvyq.repository.UserRepository;
import com.lvyq.service.*;
import com.lvyq.util.ReceContentUtils;
import com.lvyq.util.XMLUtils;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @program: weg
 * @description:
 * @author: lvyq
 * @create: 2023-03-08 10:13
 **/
@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private MongoTemplate mongoTemplate;

    @Resource
    private OrderRepository orderRepository;


    @Resource
    private ConvenientService convenientService;

    @Resource
    private ShopService shopService;

    @Resource
    private UserService userService;

    @Resource
    private MyFarmService myFarmService;

    @Resource
    private ReplyMsgService replyMsgService;

    @Resource
    private PastureService pastureService;



    /** 
    * @Description: 保存指令
    * @params:  * @param data
    * @return: {@link java.lang.Object}
    * @Author: lvyq
    * @Date: 2023/3/8 10:24
    */
    @Override
    public JSONObject saveOrder(Order data) {
        JSONObject mav = new JSONObject();
        mongoTemplate.save(data);
        mav.put("msg","操作成功");
        return mav;
    }

    /** 
    * @Description: 指令回复
    * @params:  * @param response
    * @param receiveMsg
    * @return: {@link com.alibaba.fastjson.JSONObject}
    * @Author: lvyq
    * @Date: 2023/3/8 10:47
    */
    @Override
    public  JSONObject orderReply(HttpServletResponse response, ReceiveMsg receiveMsg) throws IOException {
        JSONObject mav = new JSONObject();
        //关注
        if (receiveMsg.getMsgType().equals(MsgTypeConstant.EVENT) && receiveMsg.getEvent().equals("subscribe")){
            receiveMsg.setContent("关注");
        }
        String receContent = receiveMsg.getContent();
        //接收指令转换
        receContent=ReceContentUtils.changeContent(receContent);
        //指令查询
        Order order = orderRepository.findByOrderName(receContent);
        ReplyMsg replyMsg = new ReplyMsg();
        replyMsg.setMsgType(MsgTypeConstant.TEXT);
        replyMsg.setFromUserName(receiveMsg.getToUserName());
        replyMsg.setToUserName(receiveMsg.getFromUserName());
        replyMsg.setCreateTime(System.currentTimeMillis());
        if (order!=null){
            String replyContent=order.getMovementContent();
            //个人中心
            if (order.getOderType()==OrderTypeConstant.GRZX){
                replyContent= userService.orderReply(replyContent,receiveMsg);
            }
            //便民服务
            if (order.getOderType()== OrderTypeConstant.BMFW){
                replyContent=convenientService.orderReply(replyContent,receiveMsg);
            }

            //金币商城
            if (order.getOderType()== OrderTypeConstant.JBSC){
                replyContent=shopService.orderReply(replyContent,receiveMsg);
            }

            //小窝农场
            if (order.getOderType()== OrderTypeConstant.XWNC){
                replyContent=myFarmService.orderReply(replyContent,receiveMsg);
            }

            //小窝牧场
            if (order.getOderType()== OrderTypeConstant.XWMC){
                replyContent=pastureService.orderReply(replyContent,receiveMsg);
            }
            replyMsg.setContent(replyContent);
        }else {
            //指令未存在于指令库中
            replyMsg.setContent("/::~对不起，您说的话，小窝还没听懂，小窝正在努力学习中[GoForIt]");
        }
        String msg= XMLUtils.ObjToXml(replyMsg);
        response.setCharacterEncoding("UTF-8");
        response.getWriter().print(msg);
        replyMsgService.save(replyMsg);
        mav.put("msg","操作成功");
        return mav;
    }




}
